<?php
/**
 * Created by b17
 * 3/23/12 6:36 PM
 */

namespace Managers;

use Managers\Builder\BadWordsBuilder,
    Catalog\BadWords as Entity;

class BadWords extends BaseManager implements CrudManager
{
    protected $__table = BadWordsBuilder::TABLE;

    /**
     * Создает новую запись.
     * @param array $values
     */
    public function create(array $values)
    {
        $entity = new Entity();
        $entity->setCreatedAt(new \DateTime());
        $entity->setIsDel(false);

        $this->_setEntityValues($entity, $values);

        $this->_em->persist($entity);
        $this->flush();
        return $entity;
    }

    private function _setEntityValues(Entity $entity, array $values)
    {
        $entity->setWord($values['word']);
    }

    /**
     * Обновляет запись.
     * @param array $values
     */
    public function update(array $values)
    {
        $builder = new BadWordsBuilder();
        $entity = $builder->setId($values['id'])->getOneEntity();

        $this->_setEntityValues($entity, $values);

        $this->_em->persist($entity);
        $this->flush();
        return $entity;
    }

    /**
     * Возвращает елемент по ID, false - если елемент не найден.
     *
     * @param array|bool $id
     */
    public function getById($id)
    {
        $builder = new BadWordsBuilder();
        $word = $builder->setId($id)->getOne();
        return $word;
    }
}
